// 
// DotNetNuke?- http://www.dotnetnuke.com 
// Copyright (c) 2002-2011 
// by DotNetNuke Corporation 
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions: 
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software. 
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE. 
// 

using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Framework.Providers;

namespace CharityGamesOnline.Modules.Charity
{

    /// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// SQL Server implementation of the abstract DataProvider class 
    /// </summary> 
    /// <remarks> 
    /// </remarks> 
    /// <history> 
    /// </history> 
    /// ----------------------------------------------------------------------------- 
    public class SqlDataProvider : DataProvider
    {


        #region "Private Members"

        private const string ProviderType = "data";
        private const string ModuleQualifier = "CGO_";

        private ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration(ProviderType);
        private string _connectionString;
        private string _providerPath;
        private string _objectQualifier;
        private string _databaseOwner;

        #endregion

        #region "Constructors"

        public SqlDataProvider()
        {

            // Read the configuration specific information for this provider 
            Provider objProvider = (Provider)_providerConfiguration.Providers[_providerConfiguration.DefaultProvider];

            // Read the attributes for this provider 

            //Get Connection string from web.config 
            _connectionString = Config.GetConnectionString();

            if (_connectionString == "")
            {
                // Use connection string specified in provider 
                _connectionString = objProvider.Attributes["connectionString"];
            }

            _providerPath = objProvider.Attributes["providerPath"];

            _objectQualifier = objProvider.Attributes["objectQualifier"];
            if (_objectQualifier != "" & _objectQualifier.EndsWith("_") == false)
            {
                _objectQualifier += "_";
            }

            _databaseOwner = objProvider.Attributes["databaseOwner"];
            if (_databaseOwner != "" & _databaseOwner.EndsWith(".") == false)
            {
                _databaseOwner += ".";
            }

        }

        #endregion

        #region "Properties"

        public string ConnectionString
        {
            get { return _connectionString; }
        }

        public string ProviderPath
        {
            get { return _providerPath; }
        }

        public string ObjectQualifier
        {
            get { return _objectQualifier; }
        }

        public string DatabaseOwner
        {
            get { return _databaseOwner; }
        }

        #endregion

        #region "Private Methods"

        private string GetFullyQualifiedName(string name)
        {
            return DatabaseOwner + ObjectQualifier + ModuleQualifier + name;
        }

        private object GetNull(object Field)
        {
            return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value);
        }

        #endregion

        #region "Public Methods"
        /// <summary>
        /// 
        /// </summary>
        /// <param name="PortalID"></param>
        /// <returns></returns>
        public override IDataReader GetCharities(int PortalID, int EventID)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("CharityGetList"), PortalID, EventID);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="PortalID"></param>
        /// <param name="CharityID"></param>
        /// <returns></returns>
        public override IDataReader GetCharity(int PortalID, int CharityID)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("CharityGet"), PortalID, CharityID);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="CharityID"></param>
        /// <param name="PortalID"></param>
        /// <param name="UserID"></param>
        /// <param name="Status"></param>
        /// <param name="Name"></param>
        /// <param name="Description"></param>
        /// <param name="Contact"></param>
        /// <param name="WebSite"></param>
        /// <param name="Email"></param>
        /// <param name="Address"></param>
        /// <param name="Category"></param>
        /// <param name="LogoFile"></param>
        public override void SaveCharity(int CharityID, int PortalID, int UserID, int Status, string Name, string Description,
                                         string Contact, string WebSite, string Email, string Telephone, string Fax, string Address,
                                         string Category, string ImageURL, string ImageType, int ImageWidth, int ImageHeight, bool ImageDisplay)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("CharitySave"), 
                                       CharityID, PortalID, UserID, Status, Name,Description, Contact, WebSite, Email, Telephone, 
                                       Fax, Address, Category, ImageURL, ImageType,ImageWidth,ImageHeight,ImageDisplay);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PortalID"></param>
        /// <param name="CharityID"></param>
        public override void DeleteCharity(int PortalID, int CharityID)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("CharityDelete"), PortalID, CharityID);
        }

        #endregion

    }
}